/**
 * 预算计算工具
 */
import { formatPrice, formatDistance } from './numberFormatter'

export class BudgetCalculator {
  /**
   * 计算路线总费用
   * @param {Array} routeDetails - 路线详情
   * @returns {Object} 费用统计
   */
  static calculateTotalCost(routeDetails) {
    let totalTicketPrice = 0
    let totalTransportCost = 0
    let totalCost = 0

    routeDetails.forEach((detail) => {
      totalTicketPrice += detail.ticket_price || 0

      if (detail.from_previous) {
        totalTransportCost += detail.from_previous.cost || 0
      }
    })

    totalCost = totalTicketPrice + totalTransportCost

    return {
      totalTicketPrice: formatPrice(totalTicketPrice),
      totalTransportCost: formatPrice(totalTransportCost),
      totalCost: formatPrice(totalCost)
    }
  }

  /**
   * 计算路线总距离
   * @param {Array} routeDetails - 路线详情
   * @returns {number} 总距离（公里）
   */
  static calculateTotalDistance(routeDetails) {
    let totalDistance = 0

    routeDetails.forEach(detail => {
      if (detail.from_previous) {
        totalDistance += detail.from_previous.distance || 0
      }
    })

    return formatDistance(totalDistance)
  }

  /**
   * 计算路线总时间
   * @param {Array} routeDetails - 路线详情
   * @returns {number} 总时间（分钟）
   */
  static calculateTotalTime(routeDetails) {
    let totalTime = 0

    routeDetails.forEach(detail => {
      totalTime += detail.visit_time || 60 // 游览时间

      if (detail.from_previous) {
        totalTime += detail.from_previous.time || 0 // 交通时间
      }
    })

    return totalTime
  }

  /**
   * 格式化时间显示
   * @param {number} minutes - 分钟数
   * @returns {string} 格式化后的时间字符串
   */
  static formatTime(minutes) {
    const hours = Math.floor(minutes / 60)
    const mins = minutes % 60

    if (hours > 0) {
      return `${hours}小时${mins}分钟`
    }
    return `${mins}分钟`
  }
}

